package org.bukkit.pm.dmatch;

import java.util.logging.Level;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;

/**
 * Handle events for all Player related events
 * such as commands
 * @author patamau
 */
public class DMatchPlayerListener extends PlayerListener {
    private final DMatchPlugin _plugin;

    public DMatchPlayerListener(DMatchPlugin plugin) {
        _plugin = plugin;
    }
    
    public void onPlayerJoin(PlayerEvent event){
    	Player p = event.getPlayer();
    	if(p.isOp()){
    		if(_plugin.hasJoinInfo()){
    			_plugin.printInfo(p);
    		}
    	}else{
        	if(_plugin.hasJoinHelp()) printHelp(p,false);
    	}
    	if(_plugin.isStarted()){
    		if(_plugin.hasJoinStat()) _plugin.printStatus(p);
    		if(_plugin.hasJoinList()) _plugin.printList(p);
    	}
    	DMatchPlayer pp = _plugin.getDMPlayer(p.getName());
    	pp.startHMonitor();
    }
    
    public void onPlayerQuit(PlayerEvent event){
    	Player p = event.getPlayer();
    	DMatchPlayer pp = _plugin.getDMPlayer(p.getName());
    	pp.stopHMonitor();
    }
    
    private String getOptionDescr(StringBuffer sb, String opt, String descr){
    	sb.setLength(0);
    	sb.append(" ");
    	sb.append(ChatColor.WHITE);
    	sb.append(opt);
    	sb.append(ChatColor.RED);
    	sb.append(": ");
    	sb.append(descr);
    	return sb.toString();
	}
    
	@Override
    public void onPlayerCommand(PlayerChatEvent event)  {
    	Player player = event.getPlayer();
        String[] split = event.getMessage().split(" ");
        if(split.length<1) {
        	_plugin.log(Level.WARNING,"command received, but no content available: "+event.getMessage());
        	return; //XXX: is it possible?
        }
        //only handle dm commands
        if(!split[0].equalsIgnoreCase("/dm")) return;
        String cmd = split.length>1?split[1]:"";
        event.setCancelled(true); //prevent other plugins or the server to handle this
        if(cmd.equalsIgnoreCase("list") ||  
        		cmd.equalsIgnoreCase("rankings")){
        	if(!_plugin.isStarted()){
        		_plugin.printStatus(player);
        	}else{
        		_plugin.printList(player);
        	}
        }else if(cmd.equalsIgnoreCase("help")){
        	//print a list of available commands
        	StringBuffer sb = new StringBuffer();
        	sb.append(ChatColor.DARK_RED);
        	sb.append("[");
        	sb.append(_plugin.getName());
        	sb.append(" ");
        	sb.append(_plugin.getVersion());
        	sb.append("] ");
        	sb.append(ChatColor.RED);
        	sb.append("Syntax: ");
        	sb.append(ChatColor.WHITE);
        	sb.append("/dm <option>");
        	player.sendMessage(sb.toString());
        	//commands are simplified: please dont mind
        	player.sendMessage(getOptionDescr(sb,"list","shows players rankings"));
        	player.sendMessage(getOptionDescr(sb,"rank [<player>]","shows your rank or the <player> rank"));
        	player.sendMessage(getOptionDescr(sb,"stat","tells if deathmatch is on"));
        	if(player.isOp()){
        		player.sendMessage(getOptionDescr(sb,"on/off","toggle deathmatch"));
        		player.sendMessage(getOptionDescr(sb,"reset","clear all the players stats"));
        		player.sendMessage(getOptionDescr(sb,"info","show plugin details"));
        	}
        }else if(cmd.equalsIgnoreCase("rank")){
        	if(!_plugin.isStarted()){
        		_plugin.printStatus(player);
        	}else{
	        	//print the rank of a player
	        	String arg;
	        	//if no player name specified, use current player's name
	        	if(split.length<3) arg = player.getName();
	        	else arg = split[2]; 
	        	_plugin.printRank(arg, player);
        	}
        }else if(cmd.equalsIgnoreCase("stat") || cmd.equalsIgnoreCase("status")){
        	_plugin.printStatus(player);
        }else if(cmd.equalsIgnoreCase("start")||cmd.equalsIgnoreCase("stop")){
        	if(player.isOp()){
        		_plugin.setStarted(cmd.equalsIgnoreCase("on"));
        	}else{
        		printHelp(player,true);
        	}
        } else if(cmd.equalsIgnoreCase("pause")){
        	if(player.isOp()){
        		_plugin.setPaused(!_plugin.isPaused());
        	}else{
        		printHelp(player, true);
        	}
        }else if(cmd.equalsIgnoreCase("reset")||cmd.equalsIgnoreCase("clear")){
        	if(player.isOp()){
        		_plugin.reset();
        	}else{
        		printHelp(player,true);
        	}
        }else if(cmd.equalsIgnoreCase("info")){
        	if(player.isOp()){
        		_plugin.printInfo(player);
        	}else{
        		printHelp(player,true);
        	}
        }else{
        	printHelp(player,true);
        }
    }
	
	private void printHelp(Player player, boolean invalid){
		StringBuffer sb = new StringBuffer();
		if(invalid){
			if(_plugin.hasMessageHeader()) {
				sb.append(ChatColor.DARK_RED);
				sb.append("[DM] ");
			}
			sb.append(ChatColor.RED);
			sb.append("Invalid option. ");
		}else{ //default help message
			sb.append(ChatColor.DARK_RED);
			sb.append("[");
			sb.append(_plugin.getName());
			sb.append(" ");
			sb.append(_plugin.getVersion());
			sb.append("] ");
		}
		sb.append(ChatColor.WHITE);
		sb.append("/dm help");
		sb.append(ChatColor.RED);
		sb.append(" for a list of available options");
		player.sendMessage(sb.toString());
	}
}
